/*
 * Copyright 2007 Matt Jensen
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package org.jtell.internal.config.annotation;

import org.jtell.internal.Empties;
import org.jtell.config.annotation.Attribute;
import org.jtell.config.annotation.Event;

import java.util.Map;
import java.util.HashMap;

/**
 * <p>
 * <code>AnnotationUtils</code> provides utility methods for dealing with JTell annotation types.
 * </p>
 */
public class AnnotationUtils
{
    /**
     * <p>
     * Construct a {@link AnnotationUtils} instance.
     * </p>
     */
    private AnnotationUtils()
    {
        super();
    }

    /**
     * <p>
     * Convert the embedded {@link Attribute} annotations inside an {@link Event} annotation into a map of attribute
     * names and values.
     * </p>
     * 
     * @param eventAnnotation the event annotation.
     * @return {@link Map} of attribute names to values.
     */
    public static Map<String, String> getAttributes(final Event eventAnnotation)
    {
        // Build the attribute map.
        final Attribute[] attributeAnnotations = eventAnnotation.attributes();
        final Map<String, String> attributes;
        if (0 == attributeAnnotations.length)
        {
            attributes = Empties.EMPTY_ATTRIBUTES;
        }
        else
        {
            attributes = new HashMap<String, String>(attributeAnnotations.length);
            for (final Attribute attributeAnnotation : attributeAnnotations)
            {
                attributes.put(attributeAnnotation.key(), attributeAnnotation.value());
            }
        }
        return attributes;
    }
}
